<html>
<head>
<script>

  var currentState = "";

  function FindMusicBetaTab(callback) {
    chrome.windows.getAll({populate: true}, function(windows) {
        var pattern = 'https?\:\/\/music\.google\.com\/music\/listen.*';
        for (var window = 0; window < windows.length; window++) {
          for (var i = 0; i < windows[window].tabs.length; i++) {
            if (windows[window].tabs[i].url.match(pattern)) {
              callback(windows[window].tabs[i].id);
              return;
            }
          }
        }
        callback(null);
      });  
  }
  
    
  function sendCommand(command) {
    FindMusicBetaTab(function(tab_id) {
        if (tab_id) {
          if (command == "foreground") {
            chrome.tabs.update(tab_id, {selected: true});
          } else {
            chrome.tabs.executeScript(tab_id,
                {
                  code: "location.assign('javascript:SJBpost(\"" + command +
                        "\");void 0');",
                  allFrames: true
                });
          }
        } else {
          chrome.tabs.create({url: 'http://music.google.com/music/listen',
                              selected: true});
        }
        
      });
  }
  

  function UpdateIcon(state) {
    if (state == "Pause")
	currentState = state;
    else if (state == "Play")
	currentState = state;
    else
	currentState = state;
  }



	function UpdateIconFromPageState(tabId, changeInfo, tab) {
	    FindMusicBetaTab(function(tab_id) {
	        if (tab_id){
	          chrome.tabs.sendRequest(tab_id, "getSkipState", UpdateIcon);
	          	       chrome.tabs.getAllInWindow(null, function(tabs){
 	 	  tabs.forEach(function(tab){
 	 	  localStorage["icon"] = "ic_media_previous.png";
    		     chrome.pageAction.setIcon({"tabId": tab.id, "path":localStorage["icon"]});
	             chrome.pageAction.show(tab.id);
  		  });
		});
	          }
	        else{
	           chrome.tabs.getAllInWindow(null, function(tabs){
 	 	  tabs.forEach(function(tab){
 	 	  localStorage["icon"] = "ic_media_previous.png";
    		     chrome.pageAction.setIcon({"tabId": tab.id, "path":localStorage["icon"]});
	             chrome.pageAction.show(tab.id);
  		  });
		});
	       }

	    });
	}

  UpdateIconFromPageState();

  // React to tabs notifications by updating our icon state if needed.
  chrome.tabs.onAttached.addListener(UpdateIconFromPageState);
  chrome.tabs.onDetached.addListener(UpdateIconFromPageState);
  chrome.tabs.onCreated.addListener(UpdateIconFromPageState);
  chrome.tabs.onRemoved.addListener(UpdateIconFromPageState);
  chrome.tabs.onUpdated.addListener(UpdateIconFromPageState);
 

  chrome.pageAction.onClicked.addListener(function(tab) {
    FindMusicBetaTab(function(tab_id) {
        if (tab_id) {
          chrome.tabs.executeScript(tab_id,
              {
                code:"location.assign('javascript:SJBpost(\"previousSong\");void 0');",
                allFrames: true
              });
        } else {
          chrome.tabs.create({url: 'http://music.google.com/music/listen', selected: true});
        }
      });
  });


  // Called when the Play/Pause state changes.
  chrome.extension.onRequest.addListener(function(message, sender, callback) {
      UpdateIconFromPageState();
      callback();
    });
    



</script>
</head>
</html>
